package conceptmaniax.hotelapp.activities;

import java.text.ParseException;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TextView;
import conceptmaniax.hotelapp.config.Constants;
import conceptmaniax.hotelapp.enums.Language;
import conceptmaniax.hotelapp.managers.RequestManager;
import conceptmaniax.hotelapp.util.GeneralUtils;
import conceptmaniax.hotelapp.weather.WeatherPojo;
import conceptmaniax.hotelapp.weather.WeatherTask;
 
public class WeatherActivity extends Activity   {
			
	private ProgressDialog progressDialog;
	private Language language;
	protected Button showInfo;
	protected Button showServices;
	protected Button showGuide;
	protected Button showBooking;
	protected TableLayout callout;
	protected ListView callout_list;
	protected ScrollView homeScreen;
	protected Button requests;
	protected TextView counter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.weather);
		language = GeneralUtils.getLanguage(this);
		
		final Button go_to_home = (Button) findViewById(R.id.homeButton);
		go_to_home.setOnClickListener(new View.OnClickListener() {
		public void onClick(final View view) {
	    	final Intent homePageIntent = new Intent(Constants.HOMEPAGE_ACTIVITY);
	    		startActivity(homePageIntent);
			}
		});
		
		showInfo = (Button) findViewById(R.id.hotel_button);
		showServices = (Button) findViewById(R.id.services_button);
		showGuide = (Button) findViewById(R.id.guide_button);
		showBooking = (Button) findViewById(R.id.booking_button);
		homeScreen = (ScrollView) findViewById(R.id.weather_scrollview1);
	 	requests = (Button) findViewById(R.id.requestsButton);
		callout = (TableLayout)findViewById(R.id.callout_table);
		callout_list = (ListView)findViewById(R.id.callout_listview);
		counter = (TextView) findViewById(R.id.counter);

		if (!GeneralUtils.isOnline(this)) {
			GeneralUtils.displayAlertDialogWithMessage(this, GeneralUtils.getNoNetworkMessage(this));
		} else {
				// show progress
				this.showProgress();
				
				// start the weather async task
				WeatherTask weatherTask = new WeatherTask(this);
				weatherTask.execute();
		}
	}
	
	public ProgressDialog getProgressDialog() {
		return this.progressDialog;
	}
	
	public void showProgress() {
		this.progressDialog = new ProgressDialog(this);
		this.progressDialog.setCancelable(false);
		this.progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		switch (this.language) {
		case ENGLISH:
			this.progressDialog.setMessage(Constants.PROGRESS_DIALOG_LOADING_MESSAGE_EN);
			break;
		case GREEK:
			this.progressDialog.setMessage(Constants.PROGRESS_DIALOG_LOADING_MESSAGE_GR);
			break;
		}
		this.progressDialog.show();
	}
	
	public void setFields(WeatherPojo weatherPojo) {
		((TextView)findViewById(R.id.temperature)).setText(weatherPojo.getTemperature());
		((TextView)findViewById(R.id.today)).setText(weatherPojo.getToday());
		((TextView)findViewById(R.id.day1)).setText(weatherPojo.getDay1());
		((TextView)findViewById(R.id.day2)).setText(weatherPojo.getDay2());
		((TextView)findViewById(R.id.day3)).setText(weatherPojo.getDay3());
		// today
		((TextView)findViewById(R.id.today_high)).setText(weatherPojo.getToday_high());
		((TextView)findViewById(R.id.today_low)).setText(weatherPojo.getToday_low());
		((TextView)findViewById(R.id.today_wind)).setText(weatherPojo.getToday_wind());
		((TextView)findViewById(R.id.today_humidity)).setText(weatherPojo.getToday_humidity());
		((ImageView)findViewById(R.id.today_icon)).setBackgroundResource(getResources().getIdentifier(weatherPojo.getToday_icon(), "drawable", Constants.ACTIVITY_PREF));
		// day 1
		((TextView)findViewById(R.id.day1_high)).setText(weatherPojo.getDay1_high());
		((TextView)findViewById(R.id.day1_low)).setText(weatherPojo.getDay1_low());
		((TextView)findViewById(R.id.day1_wind)).setText(weatherPojo.getDay1_wind());
		((TextView)findViewById(R.id.day1_humidity)).setText(weatherPojo.getDay1_humidity());
		((ImageView)findViewById(R.id.day1_icon)).setBackgroundResource(getResources().getIdentifier(weatherPojo.getDay1_icon(), "drawable", Constants.ACTIVITY_PREF));
		// day 2
		((TextView)findViewById(R.id.day2_high)).setText(weatherPojo.getDay2_high());
		((TextView)findViewById(R.id.day2_low)).setText(weatherPojo.getDay2_low());
		((TextView)findViewById(R.id.day2_wind)).setText(weatherPojo.getDay2_wind());
		((TextView)findViewById(R.id.day2_humidity)).setText(weatherPojo.getDay2_humidity());
		((ImageView)findViewById(R.id.day2_icon)).setBackgroundResource(getResources().getIdentifier(weatherPojo.getDay2_icon(), "drawable", Constants.ACTIVITY_PREF));
		// day 3
		((TextView)findViewById(R.id.day3_high)).setText(weatherPojo.getDay3_high());
		((TextView)findViewById(R.id.day3_low)).setText(weatherPojo.getDay3_low());
		((TextView)findViewById(R.id.day3_wind)).setText(weatherPojo.getDay3_wind());
		((TextView)findViewById(R.id.day3_humidity)).setText(weatherPojo.getDay3_humidity());
		((ImageView)findViewById(R.id.day3_icon)).setBackgroundResource(getResources().getIdentifier(weatherPojo.getDay3_icon(), "drawable", Constants.ACTIVITY_PREF));
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		SetButtonActivity.getInstance().setRecognition(false);
		SetButtonActivity.getInstance().setButtons(this, showInfo, showServices, showGuide, showBooking, callout, callout_list, homeScreen, requests);
		try {
			int counter = RequestManager.getInstance().getRequestCount();
			this.counter.setText(""+counter);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    // manage message counter
	    if (this.counter.getText().equals("0")) 
	        this.counter.setVisibility(View.INVISIBLE);
	     else
	        this.counter.setVisibility(View.VISIBLE);
	}
	 
	@Override
	protected void onDestroy() {
		super.onDestroy();
		unbindDrawables(findViewById(R.id.weather_relative));
		System.gc();  
	}
		
	private void unbindDrawables(View view) {
		if (view.getBackground() != null) {
			view.getBackground().setCallback(null);
		}
	        
		if (view instanceof ViewGroup && !(view instanceof AdapterView)) {
			for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
				unbindDrawables(((ViewGroup) view).getChildAt(i));
			}
			((ViewGroup) view).removeAllViews();
	    }
	}
		
	@Override
	public boolean onCreateOptionsMenu(final Menu menu) {		
		return super.onCreateOptionsMenu(GeneralUtils.createOptionsMenu(this, menu));
	}
	
	@Override
	public boolean onOptionsItemSelected(final MenuItem item) {
		GeneralUtils.onOptionsItemSelected(this, item);
		return super.onOptionsItemSelected(item);
	}
	
	@Override
	protected Dialog onCreateDialog(final int id) {
		return GeneralUtils.createAboutUsDialog(this);
	}
}